<?php

namespace App\Jobs;

use App\Services\WxCard\WxCardService;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

/**
 * 卡券审核事件-队列处理
 * @package App\Jobs
 */
class WxCardCheck implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private $appid;
    protected $eventData;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($appid, $eventData)
    {
        $this->appid = $appid;
        $this->eventData = $eventData;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $wxCardService = new WxCardService($this->appid);
        $cardId = $this->eventData['CardId'];
        if ($cardId){
            $cardData = $wxCardService->getCardByCardId($cardId);
            if ($cardData){
                $wxCardService->createCardObj($cardData['card_type'])->handleCardCheck($this->eventData);
            }else{
                throw new \Exception('卡券数据不存在');
            }
        }
    }
}
